<?php
/**
 * 微信设置
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class WechatController extends AdminbaseController {
    protected $wx_model;
    protected $menu_model;
    protected $oauth_model;
    protected $user_model;
    public function _initialize() {
        parent::_initialize();
        $this->wx_model = D("Common/WxConfig");
        $this->menu_model = D("Common/WxMenu");
        $this->oauth_model = D("Common/WxOauth");
        $this->user_model = D("Common/WxUser");
    }
    /**
     * 微信配置
     */
    public function config() {
        $data = $this->wx_model->where("id=1")->find();
        $this->assign("data",$data);
       	$this->display();
    }
    /**
     * 微信配置
     */
    public function config_post(){
        $id=intval($_POST['id']);
        $config=I("post.post");
        $config['bindmsg'] = $_POST['post']['bindmsg'];
        if(empty($id)){
            $result=$this->wx_model->add($config);
            if ($result) {
                $this->success("添加成功！");
            } else {
                $this->error("添加失败！");
            }
        }else{
            $config['id']=$id;
            $result=$this->wx_model->save($config);
            if ($result) {
                $this->success("保存成功！");
            } else {
                $this->error("保存失败！");
            }
        }
    }
    
    /**
     * 自定义菜单
     */
    public function menu() {
        $ret = $this->menu_model->order(array("order" => "ASC"))->select();
        $menu = $pmenu = array();
        if($ret){
            foreach ($ret as $v){
                if($v['pid'] == 0){
                    $pmenu[] = $v;
                }else{
                    $menu[$v['pid']][] = $v;
                }
            }
            foreach($pmenu as $k=>$v){
                if(array_key_exists($v['id'],$menu)){
                    $pmenu[$k]['menu']=$menu[$v['id']];
                }
            }
        }
        $this->assign ( 'pmenu', $pmenu );
        $this->display();
    }
    /**
     * 生成菜单
     */
    public function menu_post(){
        $config = $this->wx_model->where("id=1")->find();
        $weixin = new \Weixin\Wechat($config);
        $ret = $this->menu_model->where("pid=0")->order(array("order" => "ASC"))->select();
        if($ret){
            foreach($ret as $k=>$v){
                $button[$k]['name'] = $v['name'];
                $ret2 = $this->menu_model->where("pid={$v['id']}")->order(array("order" => "ASC"))->select();
                if($ret2){
                    foreach($ret2 as $kk=>$vv){
                        $button[$k]['sub_button'][$kk]['name'] = $vv['name'];
                        if($vv['type'] == 1){
                            $button[$k]['sub_button'][$kk]['key'] = $vv['value'];
                            $button[$k]['sub_button'][$kk]['type'] = "click";
                        }elseif($vv['type'] == 3){
                            $button[$k]['sub_button'][$kk]['key'] = $vv['value'];
                            $button[$k]['sub_button'][$kk]['type'] = "click";
                        }else{
                            $vv['value'] = str_replace('{id}', $id, $vv['value']);
                            $button[$k]['sub_button'][$kk]['url'] = $vv['value'];
                            $button[$k]['sub_button'][$kk]['type'] = "view";
                        }
                    }
                }else{
                    if($v['type'] == 1){
                        $button[$k]['key'] = $v['value'];
                        $button[$k]['type'] = "click";
                    }else{
                        $v['value'] = str_replace('{id}', $id, $v['value']);
                        $button[$k]['url'] = $v['value'];
                        $button[$k]['type'] = "view";
                    }
                }
            }
        }
        $res = $weixin->createMenu(array('button'=>$button));
        if($res === false){
            $this->error("菜单生成失败！".$weixin->errMsg);
        }else{
            $this->success("菜单生成成功！");
        }
    }
    
    // 自定义菜单添加
    public function menu_add() {
        $tree = new \Tree();
        $result = $this->menu_model->where("pid=0")->order(array("order" => "ASC"))->select();
        foreach ($result as $r) {
            $r['selected'] = $r['id'] == 0 ? 'selected' : '';
            $array[] = $r;
        }
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_categorys = $tree->get_tree(0, $str);
        $this->assign("select_categorys", $select_categorys);
        $this->display();
    }
    
    // 自定义菜单添加提交
    public function menu_add_post() {
        if (IS_POST) {
            $pid = $_POST['post']['pid'];
            $count = $this->menu_model->where("pid=$pid")->count();
            if($count<5){
                $name = $_POST['post']['name'];
                $type = $_POST['post']['type'];
                if(empty($name)){
                    $this->error("菜单名称不能为空！");
                }
                if(empty($type)){
                    $this->error("菜单类型不能为空！");
                }
                $menu=I("post.post");
                $menu['value']=$_POST['post']['value'];
                $result=$this->menu_model->add($menu);
                if ($result) {
                    $this->success("添加成功！", U("wechat/menu"));
                } else {
                    $this->error("添加失败！");
                }
            }else{
                $this->error("微信一级菜单最多只能有5个二级菜单！");
            }
        }
    }
    // 自定义菜单删除
    public function menu_delete() {
        $id = I("get.id",0,'intval');
        $count = $this->menu_model->where(array("pid" => $id))->count();
        if ($count > 0) {
            $this->error("该菜单下还有子菜单，无法删除！");
        }
        if ($this->menu_model->delete($id)!==false) {
            $this->success("删除菜单成功！");
        } else {
            $this->error("删除失败！");
        }
    }
    
    // 自定义菜单编辑
    public function menu_edit() {
        $tree = new \Tree();
        $id = I("get.id",0,'intval');
        $rs = $this->menu_model->where(array("id" => $id))->find();
        
        $result = $this->menu_model->where("pid=0")->order(array("order" => "ASC"))->select();
        foreach ($result as $r) {
            $r['selected'] = $r['id'] == $rs['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_categorys = $tree->get_tree(0, $str);
        $this->assign("data", $rs);
        $this->assign("select_categorys", $select_categorys);
        $this->display();
    }
    
    // 自定义菜单编辑提交
    public function menu_edit_post() {
        if (IS_POST) {
            $menu=I("post.post");
            $menu['value']=$_POST['post']['value'];
            $result=$this->menu_model->save($menu);
            if ($result) {
                $this->success("更新成功！", U("wechat/menu"));
            } else {
                $this->error("更新失败！");
            }
        }
    }
    
    /**
     * 微信oauth设置
     */
    public function oauth(){
        $baseurl = $_SERVER['SERVER_NAME'] ? "http://".$_SERVER['SERVER_NAME']."/" : "http://".$_SERVER['HTTP_HOST']."/";
        $this->assign('baseurl',$baseurl);
        $oauth = $this->oauth_model->select();
        $this->assign('oauth',$oauth);
        $this->display();
    }
    
    /**
     * 添加oauth设置
     */
    public function oauth_add(){
        $this->display();
    }
    /**
     * 添加oauth跳转提交
     */
    public function oauth_add_post(){
        $weburl = $_POST['post']['weburl'];
        if(empty($weburl)){
            $this->error("跳转地址不能为空！");
        }
        $oauth=I("post.post");
        $oauth['weburl']=$weburl;
        $result=$this->oauth_model->add($oauth);
        if ($result) {
            $this->success("添加成功！", U("wechat/oauth"));
        } else {
            $this->error("添加失败！");
        }
    }
    // oauth删除
    public function oauth_delete() {
        $oid = I("get.id",0,'intval');
        if ($this->oauth_model->delete($oid)!==false) {
            $this->success("删除菜单成功！");
        } else {
            $this->error("删除失败！");
        }
    }
    
    // oauth编辑
    public function oauth_edit() {
        $id = I("get.id",0,'intval');
        $rs = $this->oauth_model->where(array("oid" => $id))->find();
        $this->assign("data", $rs);
        $this->display();
    }
    
    // oauth编辑提交
    public function oauth_edit_post() {
        if (IS_POST) {
            $weburl = $_POST['post']['weburl'];
            if(empty($weburl)){
                $this->error("跳转地址不能为空！");
            }
            $oauth=I("post.post");
            $oauth['weburl']=$weburl;
            $result=$this->oauth_model->save($oauth);
            if ($result) {
                $this->success("更新成功！", U("wechat/oauth"));
            } else {
                $this->error("更新失败！");
            }
        }
    }
    /**
     * 微信用户列表
     */
    public function user(){
        $count=$this->user_model->count();
        $page = $this->page($count, 12);
        $users=$this->user_model->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign("users",$users);
        $this->display();
    }
}

